package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysQuestion;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysDiagnosisTask;
import com.ruoyi.system.service.ISysDiagnosisTaskService;
import com.ruoyi.system.service.ISysQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 问题数据Controller
 * 
 * @author ruoyi
 * @date 2022-05-03
 */
@RestController
@RequestMapping("/system/question")
public class SysQuestionController extends BaseController
{
    @Autowired
    private ISysQuestionService sysQuestionService;

    @Autowired
    private ISysDiagnosisTaskService sysDiagnosisTaskService;
    /**
     * 查询问题数据列表
     */
    //@PreAuthorize("@ss.hasPermi('system:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysQuestion sysQuestion)
    {
        startPage();
        List<SysQuestion> list = sysQuestionService.selectSysQuestionList(sysQuestion);
        return getDataTable(list);
    }

    /**
     * 导出问题数据列表
     */
    //@PreAuthorize("@ss.hasPermi('system:question:export')")
    @Log(title = "问题数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysQuestion sysQuestion)
    {
        List<SysQuestion> list = sysQuestionService.selectSysQuestionList(sysQuestion);
        ExcelUtil<SysQuestion> util = new ExcelUtil<SysQuestion>(SysQuestion.class);
        util.exportExcel(response, list, "问题数据数据");
    }

//    @PreAuthorize("@ss.hasPermi('system:question:list')")
//    @GetMapping("/{questionIds}")
//    public AjaxResult getInfo(@PathVariable Long[] questionIds)
//    {
//        return AjaxResult.success(sysQuestionService.selectSysQuestionByQuestionIds(questionIds));
//    }

    /**
     * 获取问题数据详细信息
     */
   // @PreAuthorize("@ss.hasPermi('system:question:query')")
    @GetMapping(value = "/{questionId}")
    public AjaxResult getInfo(@PathVariable("questionId") Long questionId)
    {
        return AjaxResult.success(sysQuestionService.selectSysQuestionByQuestionId(questionId));
    }

    /**
     * 获得问题领域属性结构
     */
    @GetMapping(value = "/treelist")
    public AjaxResult treeList(SysQuestion sysQuestion)
    {
        return AjaxResult.success( sysQuestionService.getSurveyTreeSelect(sysQuestion));
    }
    /**
     * 获得带有回答记录的问题领域属性结构
     */
    @GetMapping(value = "/treelistwithrecord")
    public AjaxResult treeListWithRecord(SysQuestion sysQuestion)
    {
           if(sysQuestion.getNaireId()==null){
               List<SysDiagnosisTask> task =sysDiagnosisTaskService.selectSysDiagnosisTaskByCompanyId(sysQuestion.getCompanyId());
            if(task==null)
                return AjaxResult.success("尚未开始诊断");
            if(task.size()>0){
                sysQuestion.setNaireId(task.get(0).getNaireId());
            }
        }
        return AjaxResult.success( sysQuestionService.getSurveyTreeSelectWithRecord(sysQuestion));
    }
    /**
     * 新增问题数据
     */
   // @PreAuthorize("@ss.hasPermi('system:question:add')")
    @Log(title = "问题数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysQuestion sysQuestion)
    {
        return toAjax(sysQuestionService.insertSysQuestion(sysQuestion));
    }

    /**
     * 修改问题数据
     */
   // @PreAuthorize("@ss.hasPermi('system:question:edit')")
    @Log(title = "问题数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysQuestion sysQuestion)
    {
        return toAjax(sysQuestionService.updateSysQuestion(sysQuestion));
    }

    /**
     * 删除问题数据
     */
   // @PreAuthorize("@ss.hasPermi('system:question:remove')")
    @Log(title = "问题数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{questionIds}")
    public AjaxResult remove(@PathVariable Long[] questionIds)
    {
        return toAjax(sysQuestionService.deleteSysQuestionByQuestionIds(questionIds));
    }


}
